package nckd.wnqjt.tmc.tda.plugin.form;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import kd.bos.algo.DataSet;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.FilterItemInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

/**
 * @author husheng
 * @date 2024-11-04 20:08
 * @description
 */
public class GuaranteewayListDataPlugin extends AbstractReportListDataPlugin {
    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) {
        //筛选出 筛选器传入的ids
        Map<String, Object> customParam = reportQueryParam.getCustomParam();
        List<String> list = (ArrayList<String>) customParam.get("id");
        List<Long> longList = list.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());

        String guaranteecontractSql = "id,entry_guaranteeorg.id,entry_guaranteeorg.a_guaranteeorgtext," +
                "entry_guaranteeorg.a_guaranteetype,entry_guaranteeorg.a_gamount,entry_guaranteeorg.a_gratio";

        QFilter filter1 = new QFilter("billstatus", QCP.equals, "C")
                .and("entry_guaranteeorg.id", QCP.in, longList);

        // 担保合同
        DataSet gmGuaranteecontract = QueryServiceHelper.queryDataSet(this.getClass().getName(), "gm_guaranteecontract", guaranteecontractSql, filter1.toArray(), null);

        // 担保占用-担保中   担保比例为0是已解除，剩余债务金额为0是已结清，否则为担保中
        // 担保债务的债务单据类型是开票登记 cdm_payablebill，剩余债务金额不会更新值，因此票据状态是解付、拆分的开票需动态的将剩余债务金额赋值为0
        //kd.tmc.gm.formplugin.contract.GuaranteeContractUseEdit.loadGuaranteeContractUse
        QFilter gQF = new QFilter("billstatus", QCP.equals, "C")
                .and("gsrcbilltype", QCP.not_equals, "cfm_creditlimit")
                .and("gratio", QCP.not_equals, BigDecimal.ZERO)
                .and("gdebtbalance", QCP.not_equals, BigDecimal.ZERO)
                .and("gsrcbilltype", QCP.equals, "cdm_payablebill");
        DataSet gcUseBills1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "gm_guaranteeuse", "id,gcontract.id gcontractId,gdebtbalance,gsrcbillid", gQF.toArray(), null);
        QFilter filter = new QFilter("rptype", QCP.equals, "paybill")
                .and("draftbillstatus", QCP.not_in, new String[]{"payoffed", "splited"})
                .and("billstatus", QCP.equals, "C");
        DataSet cdmPayablebill = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cdm_payablebill", "id", filter.toArray(), null);
        gcUseBills1 = gcUseBills1.join(cdmPayablebill).on("gsrcbillid", "id").select(gcUseBills1.getRowMeta().getFieldNames()).finish();

        QFilter gQF2 = new QFilter("billstatus", QCP.equals, "C")
                .and("gsrcbilltype", QCP.not_equals, "cfm_creditlimit")
                .and("gratio", QCP.not_equals, BigDecimal.ZERO)
                .and("gdebtbalance", QCP.not_equals, BigDecimal.ZERO)
                .and("gsrcbilltype", QCP.not_equals, "cdm_payablebill");
        DataSet gcUseBills2 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "gm_guaranteeuse", "id,gcontract.id gcontractId,gdebtbalance,gsrcbillid", gQF2.toArray(), null);

        DataSet gcUseBills = gcUseBills1.union(gcUseBills2);
        DataSet finish = gcUseBills.groupBy(new String[]{"gcontractId"}).sum("gdebtbalance").finish();

        DataSet guaranteecontract = gmGuaranteecontract.leftJoin(finish).on("id", "gcontractId").select(gmGuaranteecontract.getRowMeta().getFieldNames(), new String[]{"gdebtbalance"}).finish();

        // 根据担保人类型分类
        DataSet guaranteecontract1 = guaranteecontract.copy().filter("entry_guaranteeorg.a_guaranteetype in ('bos_org','tmc_org')");
        DataSet guaranteecontract2 = guaranteecontract.copy().filter("entry_guaranteeorg.a_guaranteetype not in ('bos_org','tmc_org')");

        guaranteecontract1 = guaranteecontract1.select(new String[]{"entry_guaranteeorg.a_guaranteeorgtext as nckd_orgname", "entry_guaranteeorg.a_gamount as nckd_amountin", "0 nckd_amountout", "(entry_guaranteeorg.a_gratio * gdebtbalance / 100) nckd_usedamountin", "0 nckd_usedamountout"});
        guaranteecontract2 = guaranteecontract2.select(new String[]{"entry_guaranteeorg.a_guaranteeorgtext as nckd_orgname", "0 nckd_amountin", "entry_guaranteeorg.a_gamount as  nckd_amountout", "0 nckd_usedamountin", "(entry_guaranteeorg.a_gratio * gdebtbalance / 100) nckd_usedamountout"});
        guaranteecontract1 = guaranteecontract1.addField("nckd_amountin + nckd_amountout + nckd_usedamountin + nckd_usedamountout", "nckd_amountsum");
        guaranteecontract2 = guaranteecontract2.addField("nckd_amountin + nckd_amountout + nckd_usedamountin + nckd_usedamountout", "nckd_amountsum");

        DataSet dataSet = guaranteecontract1.union(guaranteecontract2);

        dataSet = dataSet.groupBy(new String[]{"nckd_orgname"}).sum("nckd_amountin").sum("nckd_amountout").sum("nckd_usedamountin").sum("nckd_usedamountout").sum("nckd_amountsum").finish();

        // 合计
        DataSet ds = dataSet.copy().groupBy(null).sum("nckd_amountin").sum("nckd_amountout").sum("nckd_usedamountin").sum("nckd_usedamountout").sum("nckd_amountsum").finish();
        ds = ds.select(new String[]{"'合计' as nckd_orgname", "nckd_amountin", "nckd_amountout", "nckd_usedamountin", "nckd_usedamountout", "nckd_amountsum"});

        return dataSet.union(ds);
    }

    private Date getCurrentDate() {
        Calendar cal = new GregorianCalendar();
//        cal.set(11, 0);
//        cal.set(12, 0);
//        cal.set(13, 0);
//        cal.set(14, 0);
        return cal.getTime();
    }
}
